﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Runtime.InteropServices;

namespace txtTEST
{
    static class Program
    {



        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {           //设置应用程序处理异常方式：ThreadException处理
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            //处理UI线程异常
            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
            //处理非UI线程异常
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);


            System.Threading.Mutex m = new System.Threading.Mutex(true, "Global\\" + Application.ProductName, out bool createNew);
            if (createNew)
            {


                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new a_itc());
            }
            else
            {
                MessageBox.Show("程序已运行!");
            }
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            StackFrame tmpSF = new StackTrace(new StackFrame(true)).GetFrame(0);

            StringBuilder sb = new StringBuilder();
            sb.AppendLine("****************************** 程序异常！请截图保存并反馈至软件工程师 ******************************");
            sb.AppendLine("异常时间：" + DateTime.Now.ToString());
            if (e.ExceptionObject as Exception != null)
            {
                sb.AppendLine("异常行号：" + tmpSF.GetFileLineNumber());
                sb.AppendLine("异常信息：" + (e.ExceptionObject as Exception).Message.Trim('。'));
                sb.AppendLine("异常位置：" + (e.ExceptionObject as Exception).StackTrace.Trim());
            }
            else
            {
                sb.AppendLine("未知异常");
            }

            MessageBox.Show(sb.ToString(), "提示");
        }

        static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
        {
            StackFrame tmpSF = new StackTrace(new StackFrame(true)).GetFrame(0);

            StringBuilder sb = new StringBuilder();
            sb.AppendLine("****************************** 程序异常！请截图保存并反馈至软件工程师 ******************************");
            sb.AppendLine("异常时间：" + DateTime.Now.ToString());
            if (e.Exception as Exception != null)
            {
                sb.AppendLine("异常行号：" + tmpSF.GetFileLineNumber());
                sb.AppendLine("异常信息：" + (e.Exception as Exception).Message.Trim('。'));
                sb.AppendLine("异常位置：" + (e.Exception as Exception).StackTrace.Trim());
            }
            else
            {
                sb.AppendLine("未知异常");
            }

            MessageBox.Show(sb.ToString(), "提示");
        }
    }
}
